package rbac_jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class StuDao {
    public static void main(String[] args) {
//        update();
        add();
    }

    public static void add(){
        Student s01 = new Student("李四",20,"男");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://192.168.111.133:3306/rbac";
            String userName = "root";
            String password = "150316";
            Connection connection = DriverManager.getConnection(url, userName, password);
            String sql = "insert into stu_info(stu_name,age,gender) values ('"+s01.getStuName()+"','"+s01.getAge()+"','"+s01.getGender()+"')";
            Statement statement = connection.createStatement();
            int rows = statement.executeUpdate(sql);
            System.out.println("rows = " + rows);
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void update(){
        //前端数据
        Student s01 = new Student("小齐",18,"女");
        s01.setId(2L);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://192.168.111.133:3306/rbac";
            String userName = "root";
            String password = "150316";
            Connection connection = DriverManager.getConnection(url, userName, password);
            String sql = "update stu_info set stu_name = '"+s01.getStuName()+"',age='"+s01.getAge()+"',gender='"+s01.getGender()+"' where id = '"+s01.getId()+"'";
            Statement statement = connection.createStatement();
            int rows = statement.executeUpdate(sql);
            System.out.println("rows = " + rows);
            connection.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
